Due Feb 24, 2:59 AM EST
Consider the following python code
1234for _ in range(4):for _ in range(8):print("Hello world!")Enter to Rename, ⇧Enter to Preview
If we run this code, how many times the phrase 'Hello world!' will be printed? Try to answer without actually running the code.
Indeed, the outer cycle runs 4 times and the inner cycle runs 8 times. Each application of the `print' command is associated with a pair consisting of the number of the run of the first cycle and the number of the run of the second cycle. By the product rule the number of such pairs is 4x8=32. So we will go through the printing line 32 times.
Consider the following python code
12345for _ in range(2):for _ in range(7):for _ in range(3):print("Hello world!")Enter to Rename, ⇧Enter to Preview
If we run this code, how many times the phrase 'Hello world!' will be printed? Try to answer without actually running the code.
Indeed, the first cycle runs 2 times, the second cycle runs 7 times and the third cycle runs 3 times. So by the application of the product rule two times we will go through the printing line 2x7x3=42 times
Consider the following python code
123456for _ in range(4):for _ in range(4):print("Hello world!")for _ in range(7):for _ in range(3):print("Hello world!")Enter to Rename, ⇧Enter to Preview
If we run this code, how many times the phrase 'Hello world!' will be printed? Try to answer without actually running the code.
By the rule of product the print function is called 4x4=16 times in the first pair of cycles. It is called 7x3=21 times in the second pair of cycle. By the rule of sum it is called 16+21=37 times in total.